package com.youlai.boot.sportPlan.controller;

import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.common.model.WeekCalendarVO;
import com.youlai.boot.sportPlan.service.SportPlanService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.sportPlan.model.form.SportPlanForm;
import com.youlai.boot.sportPlan.model.query.SportPlanQuery;
import com.youlai.boot.sportPlan.model.vo.SportPlanVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.util.List;

/**
 * 用户饮食计划前端控制层
 *
 * @author cuisheng
 * @since 2025-03-31 13:11
 */
@Tag(name = "用户饮食计划接口")
@RestController
@RequestMapping("/api/v1/sportPlans")
@RequiredArgsConstructor
public class SportPlanController  {

    private final SportPlanService sportPlanService;

    @Operation(summary = "用户饮食计划分页列表")
    @GetMapping("/page")
    public PageResult<SportPlanVO> getSportPlanPage(SportPlanQuery queryParams ) {
        queryParams.setUserId(SecurityUtils.getUserId());
        IPage<SportPlanVO> result = sportPlanService.getSportPlanPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增用户饮食计划")
    @PostMapping
    public Result<Void> saveSportPlan(@RequestBody @Valid SportPlanForm formData ) {
        formData.setUserId(SecurityUtils.getUserId());
        boolean result = sportPlanService.saveSportPlan(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取用户饮食计划表单数据")
    @GetMapping("/{id}/form")
    public Result<SportPlanForm> getSportPlanForm(
        @Parameter(description = "用户饮食计划ID") @PathVariable Long id
    ) {
        SportPlanForm formData = sportPlanService.getSportPlanFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改用户饮食计划")
    @PutMapping(value = "/{id}")
    public Result<Void> updateSportPlan(
            @Parameter(description = "用户饮食计划ID") @PathVariable Long id,
            @RequestBody @Validated SportPlanForm formData
    ) {
        boolean result = sportPlanService.updateSportPlan(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除用户饮食计划")
    @DeleteMapping("/{ids}")
    public Result<Void> deleteSportPlans(
        @Parameter(description = "用户饮食计划ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = sportPlanService.deleteSportPlans(ids);
        return Result.judge(result);
    }

    @Operation(summary = "按星期查找饮食记录日历")
    @PostMapping("/weekCalendar")
    public Result<List<WeekCalendarVO>> weekCalendar(@RequestBody List<String> dates) {
        return Result.success(sportPlanService.weekCalendar(dates));
    }
}
